//
//  DetailsViewController.m
//  dangdang
//
//  Created by air on 16/2/1.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "DetailsViewController.h"
#import "DetailsFirstTableViewCell.h"
#import "DetailsSecondTableViewCell.h"
#import "DetailsThirdTableViewCell.h"
#import "KeyboardViewController.h"

@interface DetailsViewController ()

@property (nonatomic, strong) NSDictionary * ordersDic;
@property (nonatomic, strong) NSArray * objectArray;

@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self connection];
    
    [self updateUI];
    
    self.title = @"订单详情";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)connection{
    NSString * token = userObject.accessToken;
    NSString * userIDString = userObject.userID;
    NSString * orderIDString = userObject.ordID;
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:token forKey:@"token"];
    [params setObject:userIDString forKey:@"uid"];
    [params setObject:orderIDString forKey:@"order_id"];
    
    [self postRequestWithMethodName:@"order/get_order" andParams:params andSender:nil];
}

- (void)updateUI{
    if ([_titleString isEqualToString:@"已取消"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"重新发布" style:UIBarButtonItemStylePlain target:self action:@selector(billAgain)];
    }
    
    _detailsTableView.hidden = YES;
    _detailsTableView.rowHeight = UITableViewAutomaticDimension;
    _detailsTableView.estimatedRowHeight = 44.0;
    
    _detailsTableViewConstraint.constant -= 54;
}

- (void)billAgain{
    KeyboardViewController * keyboardVC = [[UIStoryboard storyboardWithName:@"KeyboardViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"KeyboardViewController"];
    
    keyboardVC.again = @"again";
    keyboardVC.textOrSpeech = @"text";
    keyboardVC.detailsViewController = self;
    
    [self.navigationController pushViewController:keyboardVC animated:YES];
}

#pragma mark UITableViewDataSource && Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 5;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
        DetailsFirstTableViewCell * firstCell = [tableView dequeueReusableCellWithIdentifier:@"detailsFirstTableViewCell"];
        firstCell.titleLabel.text = _titleString;
        firstCell.priceLabel.text = [NSString stringWithFormat:@"￥%@", _detailsObject.exp_fee];
        
        cell = firstCell;
    }
    if (indexPath.section == 1){
        DetailsSecondTableViewCell * secondCell = [tableView dequeueReusableCellWithIdentifier:@"detailsSecondTableViewCell"];
        secondCell.firstLabel.text = labelArray[indexPath.row];
        secondCell.secondLabel.text = _objectArray[indexPath.row];
        
        cell = secondCell;
    }
    if (indexPath.section == 2){
        DetailsThirdTableViewCell * thirdCell = [tableView dequeueReusableCellWithIdentifier:@"detailsThirdTableViewCell"];
        thirdCell.nameLabel.text = [NSString stringWithFormat:@"%@ %@", _detailsObject.uname, _detailsObject.uphone];
        thirdCell.addressLabel.text = _detailsObject.location_name;
        
        cell = thirdCell;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 25.0f;
    }else{
        return 15.0f;
    }
}

- (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 2, 100, 20)];
        label.font = [UIFont systemFontOfSize:13.0];
        label.text = @"订单详情";
        label.textColor = [UIColor darkGrayColor];
        [headerView addSubview:label];
        return headerView;
    }
    return nil;
}

//网络连接返回数据
- (void)requestSuccessedWithResult:(id)result NSURLSessionTask:(NSURLSessionTask *)task sender:(id)sender{
    
    //    NSString * urlString = [NSString stringWithFormat:@"%@", task.currentRequest.URL];
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        NSString * code = [NSString stringWithFormat:@"%@", [result objectForKey:@"retcode"]];
        if ([code isEqualToString:@"0"]){
            _ordersDic = [result objectForKey:@"retmsg"];
            _detailsObject = [[OrderDetailsObject alloc] init];
            _detailsObject.exp_fee = [_ordersDic objectForKey:@"exp_fee"];
            _detailsObject.item_name = [_ordersDic objectForKey:@"item_name"];
            _detailsObject.item_price = [_ordersDic objectForKey:@"item_price"];
            _detailsObject.serial = [_ordersDic objectForKey:@"order_serial"];
            _detailsObject.time = [_ordersDic objectForKey:@"order_time"];
            _detailsObject.item_weight = [_ordersDic objectForKey:@"item_weight"];
            _detailsObject.uname = [[_ordersDic objectForKey:@"user_info"] objectForKey:@"uname"];
            _detailsObject.uphone = [[_ordersDic objectForKey:@"user_info"] objectForKey:@"uphone"];
            _detailsObject.location_name = [_ordersDic objectForKey:@"location_name"];
            _detailsObject.distance = [_ordersDic objectForKey:@"distance"];
            _detailsObject.remark = [_ordersDic objectForKey:@"remark"];
            _detailsObject.reach_time = [_ordersDic objectForKey:@"reach_time"];
            
            _objectArray = @[_detailsObject.item_name, _detailsObject.item_price, _detailsObject.serial, _detailsObject.item_weight, _detailsObject.time];
            [_detailsTableView reloadData];
            _detailsTableView.hidden = NO;
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
